package com.ykzhujiang.lovespirit.labor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ykzhujiang.lovespirit.util.Utility;

public class NankaiLabor extends Labor {

	public int Fetch(int page_num) {
			int count = 0;
			String url=url_text;
			while (count++ < page_num && url != null) {
				String text=Utility.GetURLText(url,connect_times);
				if(text==null)
				{
					return -1;
				}
				Pattern p = Pattern.compile(thread_pattern);
				Matcher m = p.matcher(text);
				while (m.find()) {
					String raw = url_root + m.group(0);
					if (!files.contains(raw)) {
						files.add(raw);
					}
				}
				p = Pattern.compile(page_pattern);
				m = p.matcher(text);
				for (int i = 0; i < 5; i++) {
					if (m.find()) {
						url = url_root + m.group(0);
					} else {
						url = null;
					}
				}
			}
			return 0;
	}

	public void SetBoard(String board) {
		this.board = board;
		thread_pattern = "bbstcon\\?board=" + board + "&file=.*?(?=>)";
		page_pattern = "bbstdoc\\?board=" + board + "&start=.*?(?=>)";
		url_text = "http://bbs.nankai.edu.cn/cgi-bin/bbs/bbstdoc?board="
				+ board;
		url_root = "http://bbs.nankai.edu.cn/cgi-bin/bbs/";
	}

	public String GetBorad() {
		return board;
	}

	private String board = "PieBridge";

	private String thread_pattern = "bbstcon\\?board=" + board
			+ "&file=.*?(?=>)";

	private String page_pattern = "bbstdoc\\?board=" + board
			+ "&start=.*?(?=>)";

	private String url_text = "http://bbs.nankai.edu.cn/cgi-bin/bbs/bbstdoc?board="
			+ board;

	private String url_root = "http://bbs.nankai.edu.cn/cgi-bin/bbs/";
}
